package com.learn;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.learn.pojo.Item;
import com.learn.repository.ItemDao;
import org.elasticsearch.index.query.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 简单的增删改查
 */
@SpringBootTest(classes = {EsSpringApplication.class})
@RunWith(SpringRunner.class)
public class EsSearchTest01 {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    @Autowired
    private ItemDao itemdao;

    /**
     * matchQuery：词条匹配，先分词然后在调用termQuery进行匹配
     *
     * TermQuery：词条匹配，不分词
     *
     * wildcardQuery：通配符匹配
     *
     * fuzzyQuery：模糊匹配
     *
     * rangeQuery：范围匹配
     *
     * booleanQuery：布尔查询
     */

    /**
     * term 查询
     * search(termQueryBuilder) 调用搜索方法，参数查询构建器对象
     */
    @Test
    public void termQuery(){
        //jpa方式 见ElasticsearchRepository使用笔记
//        List<Item> xxx = itemdao.findByTitle("中国xxx:4");
//        for (Item product : xxx) {
//            System.out.println(product);
//        }
        //https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

//        TermQueryBuilder query = QueryBuilders.termQuery("title", "中国xxx:4");
//        Iterable<Item> items = itemdao.search(query);
//        for (Item product : items) {
//            System.out.println(product);
//        }

        //https://blog.csdn.net/qq_43676902/article/details/108601002
        MatchQueryBuilder query = QueryBuilders.matchQuery("title", "中国xxx");
        NativeSearchQueryBuilder nativeSearchQueryBuilder=new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withQuery(query);
        NativeSearchQuery nativeSearchQuery=nativeSearchQueryBuilder.build();
        SearchHits<Map> ego_item = elasticsearchOperations.search(nativeSearchQuery, Map.class, IndexCoordinates.of("ego_item"));
        System.out.println(ego_item);

    }


    @Test
    public void  goods(){
        NativeSearchQueryBuilder nativeSearchQueryBuilder=new NativeSearchQueryBuilder();

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("title","中国联通:6"));
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("num");
        rangeQuery.gte(20);
        rangeQuery.lte(70);
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        nativeSearchQueryBuilder.withQuery(rangeQuery);
        NativeSearchQuery nativeSearchQuery=nativeSearchQueryBuilder.build();
        System.out.println(nativeSearchQuery.getQuery());
        SearchHits<Item> ego_item = elasticsearchOperations.search(nativeSearchQuery, Item.class, IndexCoordinates.of("ego_item"));
        ego_item.getSearchHits().forEach(System.out::println);
    }



}
